$(document).ready(function(){
	function Phrase(prefrase, phrase, colour){
		this.prefrase = prefrase;
		this.phrase = phrase;
		this.colour = colour;
	};

	function BlinkTimer(element){
		this.blink = false;
		this.element = element;
		this.stop = function(){
			console.log('this.stop');
			element.removeClass("invicible");
			this.blink = false;
		}
		this.work = function(){
			console.log('this.work' + this.blink);
			if (this.blink) {
				element.toggleClass("invicible");
				console.log('this.work inside' + this.blink);
				var _this = this;
				setTimeout(function(){_this.work();}, 500);
			}
		}
		this.start = function(){
			this.blink = true;
			this.work();
		}
	};

	var blinker = new BlinkTimer($('#cursor'));

	var phrases = [
		new Phrase("Such", "creativ and intellignt", "red"),
		new Phrase("So", "scary to close", "yellow"),
		new Phrase("Really", "think to show me to your GF", "blue"),
		new Phrase("Very", "fun when send me to friendz", "white"),
		new Phrase("Such", "doge", "red"),
		new Phrase("Such", "want to pet a pet", "black"),
		new Phrase("Such", "Mozilla", "red"),
		new Phrase("Love", "France", "blue"),
	];
	var current = 0;
	var currentPhrase = phrases[current].phrase;
	var clear = true;
	var blink = false;

	function printletter(){
		blinker.stop();
		if (currentPhrase == "") {
			current = current < phrases.length - 1 ? current + 1: 0;
			currentPhrase = phrases[current].phrase;
			clear = true;
			setTimeout(printletter, 4000);
			blinker.start();
		}
		else {
			if (clear) {
				$('#variable').text('');
				$('#prefrase').text(phrases[current].prefrase);
				$('#message').hide().fadeIn(800);
				$('#message').css({'color': phrases[current].colour});
				$('#cursor').css({'border-color': phrases[current].colour});
				clear = false;
				setTimeout(printletter, 100);
			}
			else {
				$('#variable').text($('#variable').text() + currentPhrase.charAt(0));
				currentPhrase = currentPhrase.substr(1);
				setTimeout(printletter, 100);
			}
		}
	}
	printletter();
})